library(fixest)
library(readr)
library(jiebaR)
library(readr)
library(quanteda)
library(stm)
library(keyATM)
library(readxl)
library(stargazer)
library(haven)
library(mediation)
load("Data.RData")


#Table 2. Measurement Verification: Inter- and Within-Sector Variation
v1<- feols(PRI~ProvPlan|Year+Prov+SOELevel,cluster=~Firm,Data)
v2<- feols(PRI~Finance|Year+Prov+SOELevel,cluster=~Firm,Data)
v3<- feols(PRI~RealEstate|Year+Prov+SOELevel,cluster=~Firm,Data)
v4<- feols(PRI~Marketization|Year+Sicmen+SOELevel,cluster=~Firm,Data)
v5<- feols(PRI~FixAsset|Year+Sicmen+SOELevel,cluster=~Firm,Data)
v6<- feols(PRI~Loan|Year+Sicmen+SOELevel,cluster=~Firm,Data)
v7<- feols(PRI~Marketization+FixAsset+Loan+Finance+RealEstate+ProvPlan|Year+SOELevel,cluster=~Firm,Data)
etable(v1,v2,v3,v4,v5,v6,v7,tex=TRUE,digits = 3,signif.code=c("***"=0.001, "**"=0.01, "*"=0.05,'^{+}'=0.1))

#Table 3. PRI and Poverty-Alleviation Expenditure (and Table F1 in the Appnedix)
m1<- feols(Expenditure~PRI*Post2015,cluster= ~Firm,Data)
m2<- feols(Expenditure~PRI*Post2015|Firm+Year,cluster= ~Firm,Data)
m3<- feols(Expenditure~PRI*Post2015+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Firm+Year,cluster= ~Firm,Data)
m4<- feols(Expenditure~PRI_Refine*Post2015+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Firm+Year,cluster= ~Firm,Data)
m5<- feols(Expenditure~AveragePRI*Post2015+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Firm+Year,cluster= ~Firm,Data)
etable(m1,m2,m3,m4,m5,digits = 3,tex=TRUE,signif.code=c("***"=0.001, "**"=0.01, "*"=0.05,'+'=0.1))

#Table 4. Examples of Topic-specific Political Conversations
wk<- worker()
new_user_word(wk,"十二五","n")
new_user_word(wk,"十三五","n")
new_user_word(wk,"十四五","n")
new_user_word(wk,"一带一路","n")
new_user_word(wk,"贸易战","n")
new_user_word(wk,"政策风险","n")
new_user_word(wk,"政治风险","n")
new_user_word(wk,"密切关注","n")
new_user_word(wk,"保持关注","n")
new_user_word(wk,"不确定","n")
new_user_word(wk,"不明确","n")
new_user_word(wk,"政策资源","n")
new_user_word(wk,"旅游资源","n")
new_user_word(wk,"文化资源","n")
new_user_word(wk,"经济形势","n")
new_user_word(wk,"新技术","n")
new_user_word(wk,"新领域","n")
new_user_word(wk,"证监会","n")
new_user_word(wk,"产业政策","n")
new_user_word(wk,"优惠政策","n")
new_user_word(wk,"5g","n")
new_user_word(wk,"云计算","n")
new_user_word(wk,"大湾区","n")
new_user_word(wk,"政府项目","n")

PoliQAsegwords <- as.tokens(sapply(as.character(PoliticalQA$Text), segment, wk))
PoliQAsegwords<- tokens_remove(PoliQAsegwords,pattern = stopwords::stopwords("zh", source = "misc"))
PoliQAsegwords <- dfm(PoliQAsegwords,remove_punct = TRUE, 
                      remove_symbols = TRUE,
                      remove_numbers = TRUE,
                      remove_url = TRUE,
                      remove_separators = TRUE)
PoliQAsegwords  <- dfm_remove(PoliQAsegwords, newstopwords)

KeyATMsegwords  <- dfm_remove(PoliQAsegwords,c('国家','政策','政府'))
KeyAtmdfm<- keyATM_read(dfm_trim(KeyATMsegwords,min_termfreq = 20))
keywords <- list( Resource       = c('补助','补贴','增值税','所得税'),
                    Investment    = c('规划','战略','投资','产业'),
                    Financial     = c('证监会','监管','审核','披露'),
                    Environment   = c ('环保','标准','要求','治理'),
                    International =c ('海外','美国','国际','出口'))

Keyatm10 <- keyATM(docs              = KeyAtmdfm,    # text input
                     no_keyword_topics = 5,             # number of topics without keywords
                     keywords          = keywords,       # keywords
                     model             = "base",         # select the model
                     options           = list(seed = 250))
top_words(Keyatm10, n = 5) 
dt<- make.dt(POstm10topic)
dt<- make.dt(Keyatm10)
dt$TypeSubsidy<- 0
dt$TypeSubsidy <- ifelse(dt$Topic1 > pmax(dt$Topic2, dt$Topic3, dt$Topic4, dt$Topic5, dt$Topic6, dt$Topic7, dt$Topic8, dt$Topic9, dt$Topic10), 1, 0)

dt$TypeInvestment<- 0
dt$TypeInvestment<- ifelse(dt$Topic2 > pmax(dt$Topic1, dt$Topic3, dt$Topic4, dt$Topic5, dt$Topic6, dt$Topic7, dt$Topic8, dt$Topic9, dt$Topic10), 1, 0)

dt$TypeFinancial<- 0
dt$TypeFinancial<- ifelse(dt$Topic3 > pmax(dt$Topic1, dt$Topic2, dt$Topic4, dt$Topic5, dt$Topic6, dt$Topic7, dt$Topic8, dt$Topic9, dt$Topic10), 1, 0)

dt$TypeEnvironment<- 0
dt$TypeEnvironment<- ifelse(dt$Topic4 > pmax(dt$Topic1, dt$Topic2, dt$Topic3, dt$Topic5, dt$Topic6, dt$Topic7, dt$Topic8, dt$Topic9, dt$Topic10), 1, 0)

dt$TypeIR<- 0
dt$TypeIR<- ifelse(dt$Topic5 > pmax(dt$Topic1, dt$Topic2, dt$Topic3, dt$Topic4, dt$Topic6, dt$Topic7, dt$Topic8, dt$Topic9, dt$Topic10), 1, 0)

##Table 4
table(dt$TypeSubsidy)
table(dt$TypeInvestment)
table(dt$TypeFinancial)
table(dt$TypeEnvironment)
table(dt$TypeIR)

#Table 5. Topic-specific PRI and Poverty-alleviation Expenditure (and Table F2 in the Appendix)
m1<- feols(Expenditure~PRI_Subsidy*Post2015+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Firm+Year,cluster= ~Firm,Data)
m2<- feols(Expenditure~PRI_Investment*Post2015+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Firm+Year,cluster= ~Firm,Data)
m3<- feols(Expenditure~PRI_Financial*Post2015+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Firm+Year,cluster= ~Firm,Data)
m4<- feols(Expenditure~PRI_Environment*Post2015+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Firm+Year,cluster= ~Firm,Data)
m5<- feols(Expenditure~PRI_IR*Post2015+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Firm+Year,cluster= ~Firm,Data)
m6<- feols(Expenditure~(PRI_Subsidy+PRI_Investment+PRI_Financial+PRI_Environment+PRI_IR)*Post2015+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Firm+Year,cluster= ~Firm,Data)
etable(m1,m2,m3,m4,m5,m6,tex=TRUE,digits = 3,signif.code=c("***"=0.001, "**"=0.01, "*"=0.05,'+'=0.1))

#Table 6. Heterogeneity: Preexisting Political Connection (Table F3 in the Appendix)
m1<- feols(Expenditure~PRI+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Firm+Year,cluster= ~Firm,subset(Data,Post2015==1))
m2<- feols(Expenditure~PRI+Sale+TotalAsset+TaxRate+ROE+lgAge|Firm+Year,cluster= ~Firm,subset(Data,Private==1&Post2015==1))
m3<- feols(Expenditure~PRI+Sale+TotalAsset+TaxRate+ROE+lgAge|Firm+Year,cluster= ~Firm,subset(Data,Private==0&Post2015==1))
m4<- feols(Expenditure~PRI*PoliConn+Sale+TotalAsset+TaxRate+ROE+lgAge|Firm+Year,cluster= ~Firm,subset(Data,SOELevel=='Private'&Post2015==1))
m5<- feols(Expenditure~PRI+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Firm+Year,cluster= ~Firm,subset(Data,SOELevel=='Private'&PreExistingPC==1&Post2015==1))
m6<- feols(Expenditure~PRI+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Firm+Year,cluster= ~Firm,subset(Data,SOELevel=='Private'&PreExistingPC==0&Post2015==1))
etable(m1,m2,m3,m4,m5,m6,tex=TRUE,digits = 3,signif.code=c("***"=0.001, "**"=0.01, "*"=0.05,'+'=0.1))

#Table 7. Poverty-alleviation Spending and Environmental Regulation (and Table F3 in the Appendix)
m1<- feols(EnvFineShare~Expenditure+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Firm+Year,cluster= ~Firm,subset(Data,is.na(PRI)==0&Post2015==1))
m2<- feols(EnvFineShare~Expenditure*PollutionFirmYear+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Firm+Year,cluster= ~Firm,subset(Data,is.na(PRI)==0&Post2015==1))
m3<- feols(EnvFineShare~Expenditure+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Firm+Year,cluster= ~Firm,subset(Data,is.na(PRI)==0&Post2015==1&PRIEnvHigh==0))
m4<- feols(EnvFineShare~Expenditure+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Firm+Year,cluster= ~Firm,subset(Data,is.na(PRI)==0&Post2015==1&PRIEnvHigh==1))
m5<- femlm(EnvReg~Expenditure+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Year+Firm,cluster= ~Firm,family='logit',subset(Data,is.na(PRI)==0&Post2015==1))
m6<- femlm(EnvReg~Expenditure*PollutionFirmYear+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Firm+Year,cluster= ~Firm,family='logit',subset(Data,is.na(PRI)==0&Post2015==1))
m7<- femlm(EnvReg~Expenditure+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Year+Firm,cluster= ~Firm,family='logit',subset(Data,is.na(PRI)==0&Post2015==1&PRIEnvHigh==0))
m8<- femlm(EnvReg~Expenditure+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Year+Firm,cluster= ~Firm,family='logit',subset(Data,is.na(PRI)==0&Post2015==1&PRIEnvHigh==1))
etable(m1,m2,m3,m4,m5,m6,m7,m8,tex=TRUE,digits = 3,signif.code=c("***"=0.001, "**"=0.01, "*"=0.05,'+'=0.1))
